<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <!--<el-form-item>-->
          <!--<el-input v-model="filters.username" placeholder="姓名"></el-input>-->
        <!--</el-form-item>-->
        <!--<el-form-item>-->
          <!--<el-button type="primary" v-on:click="getUsers">查询</el-button>-->
        <!--</el-form-item>-->
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="用户名" sortable>
      </el-table-column>
      <el-table-column prop="name" label="姓名"sortable>
      </el-table-column>
      <el-table-column prop="sex" label="性别" :formatter="formatSex" sortable>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="editForm.sex">
            <el-radio class="radio" :label="1">男</el-radio>
            <el-radio class="radio" :label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="editForm.password" auto-complete="off" type="password"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--新增界面-->
    <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password" auto-complete="off" type="password"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="addForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="addForm.sex">
            <el-radio class="radio" :label="1">男</el-radio>
            <el-radio class="radio" :label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<style scoped>
  .button-list {
  margin-bottom: 10px;
  }
</style>

<script>
  import {getUserlist, deleteAny, createUser, putUser} from '../apis/api'
  export default {
    data () {
      return {
        filters: {
          username: ''
        },
        users: [],
        total: 0,
        page: 1,
        listLoading: false,
        sels: [], // 列表选中列
        editFormVisible: false, // 编辑界面是否显示
        editLoading: false,
        editFormRules: {
          name: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ]
        },
        // 编辑界面数据
        editForm: {
          username: '',
          password: '',
          name: '',
          sex: -1,
          url: ''
        },
        addFormVisible: false, // 新增界面是否显示
        addLoading: false,
        addFormRules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' }
          ]
        },
        // 新增界面数据
        addForm: {
          username: '',
          name: '',
          sex: -1,
          password: ''
        }
        // endpointDB: this.$store.state.endpointDB
      }
    },
    methods: {
      getUsers () {
        let _this = this
        _this.listLoading = true
//        getUserListPage(para) 过滤也要用到
        getUserlist(this.page).then(res => {
          _this.total = res.data.count
          _this.users = res.data.results
          _this.listLoading = false
        }).catch(error => {
          console.log(error)
        })
      },
      // 性别显示转换
      formatSex: function (row, column) {
        return row.sex === 1 ? '男' : row.sex === 0 ? '女' : '未知'
      },
      handleCurrentChange (val) {
        let _this = this
        _this.page = val
        _this.getUsers()
      },
      // 删除
      handleDel: function (index, row) {
        let _this = this
        _this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          _this.listLoading = true
          // NProgress.start();
//          let para = { id: row.id }
          // 删除接口
          deleteAny(row.url).then(res => {
            _this.listLoading = false
            // NProgress.done();
            _this.$message({
              message: '删除成功',
              type: 'success'
            })
            _this.getUsers()
          })
        }).catch(() => {

        })
      },
      // 显示编辑界面
      handleEdit: function (index, row) {
        let _this = this
        _this.editFormVisible = true
        _this.editForm = Object.assign({}, row)
      },
      // 显示新增界面
      handleAdd: function () {
        let _this = this
        _this.addFormVisible = true
        _this.addForm = {
          name: '',
          sex: -1,
          age: 0,
          birth: ''
        }
      },
      // 编辑
      editSubmit: function () {
        let _this = this
        _this.$refs.editForm.validate((valid) => {
          if (valid) {
            _this.$confirm('确认提交吗？', '提示', {}).then(() => {
              _this.editLoading = true
              // NProgress.start();
//              let para = Object.assign({}, this.editForm)
              // 接口
              let updateUserData = {
                name: _this.editForm.name,
                sex: _this.editForm.sex,
                username: _this.editForm.username,
                password: _this.editForm.password
              }
              putUser(_this.editForm.url, updateUserData).then((res) => {
                _this.editLoading = false
                // NProgress.done();
                _this.$message({
                  message: '提交成功',
                  type: 'success'
                })
                _this.$refs['editForm'].resetFields()
                _this.editFormVisible = false
                _this.getUsers()
              })
            })
          }
        })
      },
      // 新增
      addSubmit: function () {
        let _this = this
        _this.$refs.addForm.validate((valid) => {
          if (valid) {
            _this.$confirm('确认提交吗？', '提示', {}).then(() => {
              _this.addLoading = true
              // NProgress.start();
//              let para = Object.assign({}, this.addForm)
              // 接口
              let createUserData = {
                name: _this.addForm.name,
                username: _this.addForm.username,
                password: _this.addForm.password,
                sex: _this.addForm.sex
              }
              createUser(createUserData).then((res) => {
                _this.addLoading = false
                // NProgress.done();
                _this.$message({
                  message: '提交成功',
                  type: 'success'
                })
                _this.$refs['addForm'].resetFields()
                _this.addFormVisible = false
                _this.getUsers()
              })
            })
          }
        })
      },
      selsChange: function (sels) {
        this.sels = sels
      },
      // 批量删除
      batchRemove: function () {
        let _this = this
//        let ids = this.sels.map(item => item.id).toString()
        _this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          _this.listLoading = true
          // NProgress.start();
//          let para = { ids: ids }
          // 接口
          _this.sels.forEach(function (item) {
            deleteAny(item.url).then((res) => {
              _this.listLoading = false
              // NProgress.done();
              _this.getUsers()
            }).catch((error) => {
              console.log(error)
            })
            _this.$message({
              message: '删除成功',
              type: 'success'
            })
//            _this.getUsers()
          })
        })
      }
    },
    mounted () {
      this.getUsers()
    }
  }
</script>
